iT邦幫忙

2023 iThome 鐵人賽

DAY 10
0

說明

前面有談到爬蟲網站抓情資,Telegram上面也有許多情資,有個網站提供許多相關的頻道,來試試看能不能爬。https://github.com/fastfire/deepdarkCTI/blob/main/telegram.md

執行

Telegram APIs

官方說有兩種方式

We offer two kinds of APIs for developers. The Bot API allows you to easily create programs that use Telegram messages for an interface. The Telegram API and TDLib allow you to build your own customized Telegram clients.

Bot API

查看邦友文章,照著建立自己的Bot
https://ithelp.ithome.com.tw/articles/10262881

跟bot的談話訊息可以用下面API取得內容

https://api.telegram.org/********:***************/getUpdates

{
  "ok": true,
  "result": [
    {
      "update_id": *******,
      "message": {
        "message_id": 3,
        "from": {
          "id": *******,,
          "is_bot": false,
          "first_name": "test",
          "last_name": "123",
          "username": "test123",
          "language_code": "zh-hans"
        },
        "chat": {
          "id": *******,,
          "first_name": "test",
          "last_name": "123",
          "username": "test123",
          "type": "private"
        },
        "date": 1695541505,
        "text": "Hello World!"
      }
    }
  ]
}

建了一個頻道測試,當沒有把Bot加入頻道就沒辦法取得訊息,但需求是加入頻道收集資訊,這方法看起來不可行。

Telegram API

https://core.telegram.org/api

取得API ID
https://core.telegram.org/api/obtaining_api_id
https://ithelp.ithome.com.tw/upload/images/20230924/20077752pjqFd3hCAU.png

使用telethon
https://docs.telethon.dev/en/stable/

!pip3 install telethon
!pip install nest_asyncio
import asyncio
from telethon.sync import TelegramClient

api_id = 'YOUR_API_ID'
api_hash = 'YOUR_API_HASH'
phone_number = 'YOUR_PHONE_NUMBER'

async def read_chat_history():
    async with TelegramClient(phone_number, api_id, api_hash) as client:
        chat_id = 't.me/xxxx'  # 替換為您要讀取的聊天的ID
        messages = await client.get_messages(chat_id, limit=10)  # 讀取最新10條消息

        for message in messages:
            print(f"{message.sender_id}: {message.text}")

if __name__ == "__main__":
    asyncio.run(read_chat_history())

https://ithelp.ithome.com.tw/upload/images/20230925/20077752NAP4DlXHAe.png


上一篇
9. 自行開發的應用程式弱點處理
下一篇
11. 攻擊面防護
系列文
珍惜生命,遠離資安 - 避免不良的資安作法,專注重要事項33
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言